package bdz.projects.atwww.electrolux.controls
{
	import bdz.projects.atwww.electrolux.events.ELEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class ELProgramRunner extends EventDispatcher {
		
		private var _timer:Timer;
		private var _duration:Number;
		private var _passed:Number = 0;
		public var analogue:Boolean = false;
		public function ELProgramRunner(target:IEventDispatcher=null) {
			super(target);
			
		}
		protected function onProgramComplete(evt:TimerEvent):void{
			dispatchEvent(new ELEvent(ELEvent.PROGRAM_COMPLETE, _duration));
		}
		protected function onProgramProgress(evt:TimerEvent):void{
			_passed++;
			
			dispatchEvent(new ELEvent(ELEvent.PROGRAM_PROGRESS, _passed));
		}
		protected function createTimer():void{
			_passed = 0;
			_timer = new Timer(1000, _duration);
			if(analogue){
				if(_duration >= 100){
					_duration = 99;
				}
			}
			_timer.addEventListener(TimerEvent.TIMER, onProgramProgress);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onProgramComplete);
		}
		public function start():void{
			if(_timer == null){
				createTimer();
			}
			_timer.start();
			
		}
		
		
		public function pause():void{
			_timer.stop();
		}
		public function reset():void{
			_timer = null;
		}
		public function get passedTime():Number{
			return _passed;
		}
		public function set passedTime(n:Number):void{
			_passed = n;
		}
		public function set duration(n:Number):void{
			
			_duration = n >= 100 && analogue ? 99 : n;
			_passed = 0;
		}
		public function get duration():Number{
			return _duration;
		}
		
	}
}